import {Platform } from 'react-native'
import Constants from "./Constants";
// import i18n from '../i18n'

const FontFamily = {
    bold:'bold',
    medium:'medium',
    regular:'regular'
}

const cutZero = (num) => {
    let old = num.toString()
    var newstr = num.toString();
    var leng = old.length - old.indexOf(".") - 1;
    if(old.indexOf(".") != -1){
        for(let i=leng;i>0;i--){
            if(newstr.lastIndexOf("0")>-1 && newstr.substr(newstr.length-1,1)==0){
                var k = newstr.lastIndexOf("0");
                if(newstr.charAt(k-1)=="."){
                    return  newstr.substring(0,k-1);
                }else{
                    newstr=newstr.substring(0,k);
                }
            }else{
                return newstr;
            }
        }
    }
    return old;
}

const numAdd = (num1, num2) => {
    var baseNum, baseNum1, baseNum2;
    try {
        baseNum1 = num1.toString().split(".")[1].length;
    } catch (e) {
        baseNum1 = 0;
    }
    try {
        baseNum2 = num2.toString().split(".")[1].length;
    } catch (e) {
        baseNum2 = 0;
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
    var precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;//精度
    return ((num1 * baseNum + num2 * baseNum) / baseNum).toFixed(precision);;
};

const getffly = (str) => {
    var fflystr = 'System';
    if(Platform.OS === 'ios')
    {
        if(str === "bold")
        {
            fflystr = "Roboto-Bold";
        }
        else if(str === "medium")
        {
            fflystr = "Roboto-Medium";
        }
        else if(str === "regular")
        {
            fflystr = "Roboto-Regular";
        }
    }
    else
    {
        if(str === "bold")
        {
            fflystr = "Roboto-Bold";
        }
        else if(str === "medium")
        {
            fflystr = "Roboto-Medium";
        }
        else if(str === "regular")
        {
            fflystr = "Roboto-Regular";
        }
    }
    return fflystr;
}

const getString = (key)=>{
    // return i18n.t(key)
    return Constants[key]
}

const setTextThousand = (num) => {

    let str = num.toString()
    if(str.indexOf(",") != -1)
    {
        return str;
    }
    else
    {
        var dqstr = "";
        var str1 = "";
        var str2 = "";
        if(str.indexOf(".") != -1)
        {
            dqstr = str.substr(0,str.indexOf("."));
            str2 = str.substr(str.indexOf("."));
        }
        else
        {
            dqstr = str;
        }

        for(var i=0;i<dqstr.length;i++)
        {
            str1 += dqstr[i];
            if((dqstr.length - 1 -i)%3 == 0 && i != dqstr.length - 1)
            {
                str1 += ",";
            }
        }
        return str1 + str2;
    }
}

const numSub = (num1, num2) => {
    var baseNum, baseNum1, baseNum2;
    try {
        baseNum1 = num1.toString().split(".")[1].length;
    } catch (e) {
        baseNum1 = 0;
    }
    try {
        baseNum2 = num2.toString().split(".")[1].length;
    } catch (e) {
        baseNum2 = 0;
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
    var precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;
    return ((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision);
};

const filterErrorMsg = (content)=>{
    content = content.replace(/Fetching/g, "Grabbing");
    content = content.replace(/fetching/g, "grabbing");
    content = content.replace(/Fetched/g, "Grabbed");
    content = content.replace(/fetched/g, "grabbed");
    content = content.replace(/Fetch/g, "Grab");
    content = content.replace(/fetch/g, "grab");
    content = content.replace(/Task/g, "Order");
    content = content.replace(/task/g, "order");
    return content
}

const ishaveSpecial = (text="") => {
    var ishave = false;
    var str1 = "<'>%*`?/&$";
    for(var i=0;i<str1.length;i++)
    {
        if(text.indexOf(str1[i]) != -1)
        {
            ishave = true;
            break;
        }
    }
    return ishave;
}
const ishaveSpecialA = (text="",str = "<'>`") => {
    var ishave = false;
    var str1 = str;
    for(var i=0;i<str1.length;i++)
    {
        if(text.indexOf(str1[i]) != -1)
        {
            ishave = true;
            break;
        }
    }
    return ishave;
}

const isEmail = (str)=>{
    let isEmail = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/.test(str);
    return isEmail
}
const numDiv = (num1, num2) => {
    var baseNum1 = 0, baseNum2 = 0;
    var baseNum3, baseNum4;
    try {
        baseNum1 = num1.toString().split(".")[1].length;
    } catch (e) {
        baseNum1 = 0;
    }
    try {
        baseNum2 = num2.toString().split(".")[1].length;
    } catch (e) {
        baseNum2 = 0;
    }
    // with (Math) {
    //     baseNum3 = Number(num1.toString().replace(".", ""));
    //     baseNum4 = Number(num2.toString().replace(".", ""));
    //     return (baseNum3 / baseNum4) * pow(10, baseNum2 - baseNum1);
    // }
    baseNum3 = Number(num1.toString().replace(".", ""));
    baseNum4 = Number(num2.toString().replace(".", ""));
    return (baseNum3 / baseNum4) * Math.pow(10, baseNum2 - baseNum1);
}
const numMulti = (num1, num2) => {
    var baseNum = 0;
    try {
        baseNum += num1.toString().split(".")[1].length;
    } catch (e) {

    }
    try {
        baseNum += num2.toString().split(".")[1].length;
    } catch (e) {

    }
    return Number(num1.toString().replace(".", "")) * Number(num2.toString().replace(".", "")) / Math.pow(10, baseNum);
};
//APP常用参数
const AppInfo = {
    channel:'',
    helpdata:null,
    activityShareFission:null
}

export  {

    getffly,
    getString,
    cutZero,
    numAdd,
    numSub,
    numDiv,
    numMulti,
    filterErrorMsg,
    FontFamily,
    setTextThousand,
    ishaveSpecial,
    ishaveSpecialA,
    isEmail,
    AppInfo


}
